package com.haixiaoke.smartLock.tianwanghulian;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.haixiaoke.vo.TwhlDataVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 天网互联-事件回调
 */
@Service
public class SmartLockTwhlCallBackService {

    private static final Logger log = LoggerFactory.getLogger(SmartLockTwhlCallBackService.class);
    @Autowired
    private SmartLockTwhlLogEventHandleService smartLockTwhlLogEventHandleService;

    public String lockCallback(Map<String,Object> map) {
        try {

            Integer productType = (Integer) map.get("productType");
            if (productType != 1) {
                log.warn("不是门锁内容推送");
                return "ok";
            }

            Object data = map.get("data");
            if (ObjectUtil.isNull(data)){
                log.warn("推送内容为空");
                return "ok";
            }

            Integer type = (Integer) map.get("type");
            TwhlDataVo vo = new TwhlDataVo();
            if (type != 6){
                vo = Convert.convert(TwhlDataVo.class, data);
            }
            switch (type) {
                case 1:// 心跳日志
                    log.info("天网互联, 智能锁心跳日志回调------data={}", JSON.toJSONString(data));
                    smartLockTwhlLogEventHandleService.smartLockInfoLog(vo);
                    break;
                case 2:// 告警日志
                    log.info("天网互联, 智能锁告警日志回调------data={}", JSON.toJSONString(data));
                    smartLockTwhlLogEventHandleService.smartLockWarningLog(vo);
                    break;
                case 3:// 下发指令回调
                    log.info("天网互联, 智能锁下发指令日志回调------data={}", JSON.toJSONString(data));
                    smartLockTwhlLogEventHandleService.smartLockCommandLog(vo);
                    break;
                case 4:// 开锁日志
                    log.info("天网互联, 智能锁开锁日志回调------data={}", JSON.toJSONString(data));
                    smartLockTwhlLogEventHandleService.smartLockUnlockLog(vo);
                    break;
                case 5:// 离线推送
                    log.info("天网互联, 智能锁离线日志回调------data={}", JSON.toJSONString(data));
                    smartLockTwhlLogEventHandleService.smartLockOfflineLog(vo);
                    break;
                case 6:// 离线开锁记录
                    log.info("天网互联, 智能锁离线开锁日志回调------data={}", JSON.toJSONString(data));
                    List<Map<String,Object>> list = Convert.convert(List.class, data);
                    smartLockTwhlLogEventHandleService.smartLockOfflineUnlockLog(list);
                    break;
                default:
                    log.warn("天网互联, 智能锁回调事件处理不在该类型中 = {}", type);
                    break;
            }
            return "ok";
        } catch (Exception e) {
            log.error("天网互联, 智能锁回调事件异常" + e.getMessage(), e);
        }
        return "ok";
    }
}
